Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • generating new variable from panel data.*

    I have multiple observations for individuals. My data is long. I have a columns for whether the event occurred during that observation (1 = yes and 0= No). I would now like to know how to generate a new variable to describe if the event EVER occurred (Yes) or NEVER occurred (No). Can anyone tell me how to do this. I've tried watching some stata youtube videos on panel data but I still can not solve this problem. Below is an example of my data (it is unbalanced). Sorry if my question is basic and simple - I'm new to Stata. Many thanks in advance.
    ID Age Event
    1 34 0
    1 39 1
    1 42 0
    2 35 0
    2 40 0
    2 50 0
    3 55 1
    3 56 0
    3 57 1

  • #2
    Here is one approach that requires installing -rangestat- from SSC:

    Code:
    clear
    input ID Age Event
    1 34 0
    1 39 1
    1 42 0
    2 35 0
    2 40 0
    2 50 0
    3 55 1
    3 56 0
    3 57 1
    end
    
    ssc install rangestat, replace
    
    rangestat (sum) Event, interval(ID 0 0)
    
    * List cases with 1 or more events.
    list if Event_sum > 0, sepby(ID) noobs
    Red Owl
    Stata/IC 16.0 (Windows 10, 64-bit)

    Comment


    • #3
      Roshani:
      welcome to this forum.
      Do you mean something along the following lines?
      Code:
      . set obs 6
      number of observations (_N) was 0, now 6
      
      . g id=1 in 1/3
      
      . replace id=2 if id==.
      
      . g event=0 in 1/3
      
      . replace event=1 in 4/5
      
      . replace event=0 if event==.
      
      . bysort id: egen flag=max(event)
      
      . list
      
           +-------------------+
           | id   event   flag |
           |-------------------|
        1. |  1       0      0 |
        2. |  1       0      0 |
        3. |  1       0      0 |
        4. |  2       1      1 |
        5. |  2       1      1 |
           |-------------------|
        6. |  2       0      1 |
           +-------------------+
      
      .
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        Dear Carlo, Yes that is exactly what I mean. Very many thanks!

        Comment


        • #5
          Sorry one more question (still using same dataset above but now with "flag" variable- I would like to compare if there is a statically significant difference between the median ages for variable flag (flag =0 and flag =1); what is the best command to use for this please?

          Comment


          • #6
            Not exactly related to the median (but related to ranks and it can convey the median if variance is similar), you could -egen - with "tag" so as to select only one observation per id, then perform a -ranksum - test for flag 1 versus 0 "if tag ==1"
            Best regards,

            Marcos

            Comment


            • #7
              Household ID sex of Household member 1 sex of Household member 2 female male
              q female female 2 0
              a male female 1 1
              I have census data for household members. I dont have the female and male variable. i want to generate these variables counting the female and males from the household members. Can you help me ?

              Comment


              • #8
                Are your sex variables string or numeric with value labels? How many sex variables do you have? See FAQ Advice #12 for how to give a data example concretely.

                Comment


                • #9
                  Originally posted by Nick Cox View Post
                  Are your sex variables string or numeric with value labels? How many sex variables do you have? See FAQ Advice #12 for how to give a data example concretely.
                  Sex variables are numeric with value labels. I have 17 sex variables for 17 house members

                  Comment


                  • #10
                    Something like

                    Code:
                    gen male = 0
                    gen female = 0
                    
                    forval j = 1/17 {
                       replace male = male + (sex`j’ == 1)
                       replace female = female + (sex`j’ == 2)
                    }
                    Notes:

                    1. Single quote characters should be LEFT SINGLE QUOTE text RIGHT SINGLE QUOTE

                    2. Assumed that your variables are named sex1 up.

                    2. Assumed that male is coded 1 and female 2

                    The code will need to be changed otherwise.

                    See also the anycount() function of egen.

                    Please flag your cross-posting on Stack Overflow.
                    Last edited by Nick Cox; 11 Sep 2023, 09:17.

                    Comment

                    Working...
                    X